package com.qf.service.impl;

import com.qf.mapper.CourseMapper;
import com.qf.pojo.Course;
import com.qf.service.CourseService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseMapper courseMapper;

    /**
     * 全查课程
     * @return
     */
    @Override
    public ResultMsg selectAll(Integer page,Integer limit) {
        if (page!=null){
           page= page<=0?1:page;
        }
        List<Course> courses = courseMapper.selectAll(page==null?null:(page-1)*limit,limit);
        if (courses!=null){
            //查询到了
            //查询所有行数，为分页准备，行数存在属性的ID里
            return new ResultMsg(0,"查询全部课程信息成功",courses,courseMapper.selectAllRows().getCourseId());
        }else {
            return new ResultMsg(200,"查询全部课程信息失败",null,0);
        }
    }

    /**
     * 根据课程ID查询一条课程信息
     * @param courseId
     * @return
     */
    @Override
    public ResultMsg selectOne(Integer courseId) {
       Course course = courseMapper.selectOne(courseId);
       if (course!=null){
           //查到了
           return ResultMsg.SUCCESS(0,course,1);
       }else {
           //没查到
        return ResultMsg.FAILED(201,"查询课程信息失败");
       }
    }

    /**
     * 根据课程ID修改课程状态
     * @param courseId
     * @return
     */
    @Override
    public ResultMsg changeCourseStatus(Integer courseId) {
        //先查出来原来的课程状态
        Course bstatus = courseMapper.selectOne(courseId);
        int status=-1;
        if (bstatus.getCourseStatus()==0){
            status=1;
        }else if (bstatus.getCourseStatus()==1){
            status=0;
        }
        Integer i=courseMapper.changeCourseStatus(courseId,status);
        if (i==1){
            //success
           return new ResultMsg(0,"修改课程状态成功",null,0);
        }else {
            return ResultMsg.FAILED(203,"修改课程状态失败");
        }
    }

    /**
     * 修改课程信息
     * @param course
     * @return
     */
    @Override
    public ResultMsg changeCourse(Course course) {
       int i= courseMapper.changeCourse(course);
        if (i==1){
            //success
            Course course1 = courseMapper.selectOne(course.getCourseId());
            return new ResultMsg(0,"修改课程信息成功",course1,1);
        }else {
            return ResultMsg.FAILED(202,"修改课程信息失败");
        }
    }

    /**
     * 删除一门课程
     * @param courseId
     * @return
     */
    @Override
    public ResultMsg deleteCourse(Integer courseId) {
        int i = courseMapper.deleteCourse(courseId);
        if (i==1){
            //success
            return new ResultMsg(0,"删除课程成功",null,0);
        }else {
            return ResultMsg.FAILED(204,"删除课程失败");
        }
    }

    /**
     * 模糊查询（根据年级名称和状态）
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResultMsg find(String name, String status) {
        if (status!=null){
            Integer cstatus=null;
            if (status.equals("0")){
                cstatus=0;
            }else if (status.equals("1")){
                cstatus=1;
            }
            Course course = new Course();
            course.setCourseName(name);
            course.setCourseStatus(cstatus);
            List<Course> courses = courseMapper.find(course);
            if (courses!=null){
                return new ResultMsg(0,"查询成功",courses,courses.size());
            }else {
                return new ResultMsg(205,"查询失败",null,0);
            }
        }else {
            Course course = new Course();
            course.setCourseName(name);
            List<Course> courses = courseMapper.find(course);
            if (courses!=null){
                return new ResultMsg(0,"查询成功",courses,courses.size());
            }else {
                return new ResultMsg(205,"查询失败",null,0);
            }
        }
    }

    /**
     * 新增一门课程
     * @param course
     * @return
     */
    @Override
    public ResultMsg add(Course course) {
        int i=courseMapper.add(course);
        if (i==1){
            //success
            return new ResultMsg(0,"新增课程成功",null,0);
        }else {
            return ResultMsg.FAILED(206,"新增课程失败");
        }
    }
}
